// 用队列实现栈
// push pop top empty
function line() {
    this.stack = []
}
line.prototype.pop = function () {
    let popel = this.stack[this.stack.length - 1];
    this.stack.length = this.stack.length - 1;
    return popel;
}
line.prototype.push = function (x) {
    this.stack[this.stack.length] = x;
    return this.stack.length;
}
line.prototype.top = function () {
    return this.stack[this.stack.length - 1]
}


// 用栈实现队列
// push pop  peek empty
function stack() {
    this.line1 = [];
    this.line2 = [];
}

stack.prototype.push = function (x) {
    this.line1.push(x)
}
stack.prototype.pop = function () {
    // 热水变凉一样
    while (this.line1.length) {
        this.line2.push(this.line1.pop())
    }
    let item = this.line2.pop()
    if(this.line2.length){
        while(this.line2.length){
            this.line1.push(this.line2.pop())
        }
    }
    return item
}
// 取值不删除值
stack.prototype.peek = function () {
    while (this.line1.length) {
        this.line2.push(this.line1.pop())
    }
    let item = this.line2[this.line2.length]
    if(this.line2.length){
        while(this.line2.length){
            this.line1.push(this.line2.pop())
        }
    }
    return item
}

// 实现包含min的栈
function minStack() {
    this.stack = [];
    this.min = [];
}
minStack.prototype.push = function(x){
    this.stack.push(x);
    this.min.push(Math.max(this.min[this.min.length-1],x))
}
minStack.prototype.pop = function(){
    let item = this.stack.pop();
    this.min.pop()
    return item
}
minStack.prototype.getmin = function(){
    return this.min[this.min.length-1]
}   


// 数组中第k个最大的数
let findK = function (nums, k) {
    nums.sort((a, b) => a - b).slice(0, k);
    return nums[k - 1]
}
